package com.spring.nio.core.service.frame;

import lombok.Data;

/**
 * @author: YSFY
 * @date: 2021-04-18 21:01
 * @pacage_name: com.spring.nio.core.service.frame
 * @class_name: FileUploadFrame
 * @description: file 文件上传帧
 */

@Data
public class FileFrame {
    // 帧字节组成及大小:
    // 4B -> 当前帧的总长度；
    // 1B -> 文件是否读取完[0:未读取完, 1:读取完],
    // 4B -> 包序号
    // 3B -> 帧类型，文件类型，文件操作类型
    // 8B -> 文件大小；
    // 2 + ? -> 剩余未文件流真实数据长度, 其中2为发送数据的长度(int表示占据4字节)
    public static final Integer FRAME_COMMON_LENGTH = 4 + 1 + 4 + 3 + 2 + 8;

    /**
     * 帧总长度
     * */
    private Integer frameSumLength;

    /**
     * 是否是结束帧
     * */
    private Byte isEnd;

    /**
     * 帧(包)序号
     * */
    private Integer frameIndex;

    /**
     * 文件帧类型
     * */
    private FrameType frameType;

    /**
     * 文件类型
     * */
    private FileType fileType;

    /**
     * 文件操作类型
     * */
    private FileOperateType fileOperateType;

    /**
     * 文件大小
     * */
    private Long fileLength;

    /**
     * 文件上传数据
     * */
    private byte[] request;

    /**
     * 帧类型 --> 1B
     */
    public enum FrameType {
        UPLOAD("UPLOAD", "上传帧", "00000000"),
        DOWNLOAD("DOWNLOAD", "下载帧", "00000001"),
        ONLINE_TRANSPORT("ONLINE_TRANSPORT", "在线传输", "00000010"),
        DELETE("DELETE", "删除帧", "00000011"),
        // 文件流数据传输帧
        DATA_TRANSPORT("DATA_TRANSPORT", "实时文件流数据帧", "00000100"),
        // 文件流数据结束帧
        DATA_TRANSPORT_END("DATA_TRANSPORT_END", "文件流结束帧", "00000101"),
        // 上传确认帧
        UPLOAD_TRANSPORT_CONFIRM("UPLOAD_TRANSPORT_CONFIRM", "上传确认帧", "00000110");

        private String frameType;

        private String description;

        private String bit;

        public String getFrameType() {
            return frameType;
        }

        public String getDescription() {
            return description;
        }

        public String getBit() {
            return bit;
        }

        FrameType(String frameType, String description, String bit) {

            this.frameType = frameType;
            this.description = description;
            this.bit = bit;
        }
    }

    /**
     * 文件类型 1B
     * */
    public enum FileType {
        MP4("mp4", "上传帧", "00000000"),
        AVI("avi", "下载帧", "00000001"),
        RMVB("rmvb", "下载帧", "00000010"),

        TXT("txt", "下载帧", "00000011"),
        XLSX("xlsx", "下载帧", "00000100"),
        XLS("xls", "下载帧", "00000101"),
        WORD("word", "下载帧", "00000110"),
        TAR("tar", "下载帧", "00000111"),
        ZIP("zip", "下载帧", "00001000"),
        EXE("exe", "下载帧", "00001001"),
        XLN("xln", "下载帧", "00001010"),
        JAVA("java", "下载帧", "00001011"),

        ALL("ALL", "共有帧", "11111111");

        private String fileType;

        private String description;

        private String bit;

        public String getFileType() {
            return fileType;
        }

        public String getDescription() {
            return description;
        }

        public String getBit() {
            return bit;
        }

        FileType(String fileType, String description, String bit) {

            this.fileType = fileType;
            this.description = description;
            this.bit = bit;
        }
    }

    /**
     * 文件操作类型 1B
     * */
    public enum FileOperateType {
        TRANSPORT("TRANSPORT", "文件传送", "00000000"),
        STORE("STORE", "文件存储", "00000001");

        private String frameType;

        private String description;

        private String bit;

        public String getFrameType() {
            return frameType;
        }

        public String getDescription() {
            return description;
        }

        public String getBit() {
            return bit;
        }

        FileOperateType(String frameType, String description, String bit) {
            this.frameType = frameType;
            this.description = description;
            this.bit = bit;
        }
    }
}
